package com.zhida.smartcontrol.dialog.nbdialog;

import android.text.TextUtils;

import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.LogUtil;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NBDialogPresenter extends BasePresenterImpl<NBDialogContract.View> implements NBDialogContract.Presenter {
    private StringBuilder mSbWeek = new StringBuilder();

    @Override
    public void requestNbTime(int switchType, List<LampSwitchBean> codeList, String[] timeArr, List<ButtonStyle> weekList, String[] bright) {
        List<String> list = new ArrayList<>();
        for (LampSwitchBean lampBean : codeList) {
            if (switchType == Const.ZIG_BEE_SINGLE) {
                list.add(lampBean.getId());
            } else {
                list.add(lampBean.getCode());
            }
        }

        String checkedWaysValue = "";
        String checkedWaysString = "";
        if (switchType == Const.NB_DOUBLE) {
            LampSwitchBean bean = codeList.get(0);
            if (bean.isCheck()) {
                checkedWaysValue = "3";
                checkedWaysString = "1,2";
            } else if (bean.isCheckMain() && !bean.isCheckAux()) {
                checkedWaysValue = "1";
                checkedWaysString = "1";
            } else if (!bean.isCheckMain() && bean.isCheckAux()) {
                checkedWaysValue = "2";
                checkedWaysString = "2";
            }
        }

        String mWeek = getStr(weekList, mSbWeek);

        if (TextUtils.isEmpty(mWeek)) {
            mView.showMsg("请选择日期");
            return;
        }

        int weekSum = getSum(mWeek);
        LogUtil.e(TAG, mWeek + "==" + weekSum);

        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);

        params.put("firstStartTime", timeArr[0]);
        params.put("firstEndTime", timeArr[1]);
        params.put("secondStartTime", timeArr[2]);
        params.put("secondEndTime", timeArr[3]);
        params.put("thirdStartTime", timeArr[4]);
        params.put("thirdEndTime", timeArr[5]);
        params.put("fourthStartTime", timeArr[6]);
        params.put("fourthEndTime", timeArr[7]);

        String url;
        if (switchType == Const.ZIG_BEE_SINGLE) {
            params.put("checkedWaysValue", "");
            params.put("checkedWaysString", "");
            url = Api.ZIGBEE_CONTROL_TIMES;
        } else {
            params.put("checkedWaysValue", checkedWaysValue); //NB单灯只有一个通道，传递空字符串即可；NB双灯主灯通道值为 1，辅灯通道值为 2，全选时和为 3
            params.put("checkedWaysString", checkedWaysString); //NB单灯只有一个通道，传递空字符串即可；NB双灯主辅灯通道全选时为 1,2
            url = Api.NB_CONTROL_TIMES;
        }

        params.put("weekdayValues", weekSum);
        params.put("checkedWeekdaysString", "" + mWeek);

        params.put("brightNess1", bright[0]);
        params.put("brightNess2", bright[1]);
        params.put("brightNess3", bright[2]);
        params.put("brightNess4", bright[3]);

//        LogUtil.e(TAG, "发送：" + JSON.toJSONString(params));

        mKalleCenter.postJson(url, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    // true表示通道
    private String getStr(List<ButtonStyle> btnList, StringBuilder sb) {
        sb.delete(0, sb.length());
        int size = btnList.size();
        for (int i = 0; i < size; i++) {
            ButtonStyle btn = btnList.get(i);
            if (btn.isChecked()) {
                sb.append((int) Math.pow(2, i)).append(",");
            }
        }
        String str = sb.toString();
        if (!TextUtils.isEmpty(str)) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }

    private int getSum(String str) {
        String[] strArr = str.split(",");
        int sum = 0;
        for (String s : strArr) {
            int anInt = Integer.parseInt(s);
            sum += anInt;
        }
        return sum;
    }


}
